package com.millstein.tsinglog.controller;

import com.millstein.tsinglog.annotation.ResponseResult;
import com.millstein.tsinglog.annotation.ShiroAccessLog;
import com.millstein.tsinglog.model.dto.notice.NoticeSaveDTO;
import com.millstein.tsinglog.model.pojo.Notice;
import com.millstein.tsinglog.model.vo.result.PageResult;

import com.millstein.tsinglog.service.NoticeService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/notice")
@ResponseResult
@CrossOrigin
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 分页并条件查询公告信息
     * @param currentPage
     * @param pageSize
     * @param keyword
     * @param type
     * @return
     */
    @GetMapping("/getNoticeByPage")
    @RequiresPermissions("notice:query")
    @ShiroAccessLog("查询公告信息")
    public PageResult<Notice> getNoticeByPage(Integer currentPage,
                                              Integer pageSize,
                                              String keyword,
                                              Short type) {
        return noticeService.getNoticeByPage(currentPage, pageSize, keyword, type);
    }

    /**
     * 获取所有启用的后台公告信息
     * @return
     */
    @GetMapping("/getAllBackUsableNotice")
    public List<Notice> getAllBackUsableNotice() {
        return noticeService.getAllBackUsableNotice();
    }

    /**
     * 根据公告类型查询已启用的数量
     * @param type 公告类型
     * @return
     */
    @GetMapping("/getNoticeCountByType")
    @RequiresPermissions(value= {"notice:insert", "notice:update"}, logical = Logical.OR)
    public int getNoticeCountByType(Short type) {
        return noticeService.getNoticeCountByType(type);
    }

    /**
     * 根据id修改指定公告是否可用
     * @param id 公告id
     * @param usable 是否可用，1可用，0不可用
     * @return
     */
    @GetMapping("/changeUsable")
    @RequiresPermissions("notice:update")
    @ShiroAccessLog("修改公告的可用状态")
    public void changeUsable(Long id, Short usable) {
        noticeService.changeUsable(id, usable);
    }

    /**
     * 根据ids删除对应的公告信息
     * @param ids 要删除的公告对应的id的集合
     * @return
     */
    @GetMapping("/deleteByIds")
    @RequiresPermissions("notice:delete")
    @ShiroAccessLog("删除公告信息")
    public void deleteByIds(@RequestParam List<Long> ids) {
        noticeService.deleteByIds(ids);
    }

    /**
     * 新增公告信息
     * @param noticeSaveDTO 新增的公告信息
     */
    @PostMapping("/insertNotice")
    @RequiresPermissions("notice:insert")
    @ShiroAccessLog("新增公告信息")
    public void insertNotice(@RequestBody @Valid NoticeSaveDTO noticeSaveDTO) {
        noticeService.insertNotice(noticeSaveDTO);
    }

    /**
     * 修改公告信息
     * @param noticeSaveDTO 修改的公告信息
     */
    @PostMapping("/updateNotice")
    @RequiresPermissions("notice:update")
    @ShiroAccessLog("修改公告信息")
    public void updateNotice(@RequestBody @Valid NoticeSaveDTO noticeSaveDTO) {
        noticeService.updateNotice(noticeSaveDTO);
    }

    /**
     * 根据id查找对应的公告信息
     * @param id
     * @return
     */
    @GetMapping("/getNoticeById")
    @RequiresPermissions("notice:update")
    public Notice getNoticeById(Long id) {
        return noticeService.getNoticeById(id);
    }
}
